﻿using System.Collections.Generic;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Map.Basic.Conditions;

namespace Ycjw.PipeLine.Map.Basic
{
    /****************************************************************************************/
    /*
    功    能:
    作    者: RenZachary
    联系方式: rzcgis@foxmail.com（QQ455745253）
    生成日期: 2021年3月24日
    修改日志: 
    版权说明: @2021 西安云创经纬信息科技有限公司 版权所有 All right reserved.
    注    意: 本内容仅限于云创经纬公司内部使用，禁止用于商业目的。
    */
    /****************************************************************************************/
    public partial class ConditionForm : Sunny.UI.UIForm
    {
        IMessageInterface m_MessageInterface;
        public IMessageInterface MessageInterface
        {
            get { return m_MessageInterface; }
            set
            {
                m_MessageInterface = value;
            }
        }
        DockPanel m_DockPanel;
        private readonly ConditionPanel m_ConditionPanel;
        public SortedDictionary<string, object> Conditions
        {
            get
            {
                return m_ConditionPanel.BaseConditions;
            }
        }

        public DockPanel DockPanel { get => m_DockPanel; set => m_DockPanel = value; }

        public ConditionForm(ConditionPanel conditionPanel)
        {
            InitializeComponent();
            if (conditionPanel is BasicToolBar)
            {
                this.Height = 80;
                uiPanelFooter.Visible = false;
            }
            else
            {
                this.SizeChanged += ConditionForm_SizeChanged;
            }
            m_ConditionPanel = conditionPanel;
            m_ConditionPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            uiPanelContainer.Controls.Add(m_ConditionPanel);
            this.m_BtnCancel.Click += BtnOKCancel_Click;
            this.m_BtnOK.Click += BtnOKCancel_Click;

            var iconPath = Commons.Paths.ApplicationResourcesDir + "\\conditions.ico";
            if (System.IO.File.Exists(iconPath))
            {
                this.ShowIcon = true;
                this.ShowTitleIcon = true;
                this.Icon = new System.Drawing.Icon(iconPath);
            }
        }
        /// <summary>
        /// 窗口大小发生变化的时候调整两个按钮的位置
        /// ■■【确定按钮】■【取消按钮】■■
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConditionForm_SizeChanged(object sender, System.EventArgs e)
        {
            var btnsWidth = this.m_BtnOK.Width + this.m_BtnCancel.Width;
            this.m_BtnOK.Left = 2 * (this.Width - btnsWidth) / 5;
            this.m_BtnCancel.Left = this.m_BtnOK.Width + 3 * (this.Width - btnsWidth) / 5;
        }
        /// <summary>
        /// 关闭事件，出发子控件ConditionPanel的AfterClose事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(System.EventArgs e)
        {
            base.OnClosed(e);
            m_ConditionPanel.AfterClose();
        }
        /// <summary>
        /// 点击按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnOKCancel_Click(object sender, System.EventArgs e)
        {
            this.DialogResult = ((Sunny.UI.UISymbolButton)sender).DialogResult;
            if (DialogResult == System.Windows.Forms.DialogResult.Cancel)
            {
                this.Close();
                m_ConditionPanel.AfterCloseWithCancel();
                return;
            }
            if (!this.m_ConditionPanel.ConditionValid())
            {
                this.m_ConditionPanel.IsConditionValid = true;
                return;
            }

            if (DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                m_ConditionPanel.SetConditions();
                this.m_MessageInterface.ShowInfo("开始执行...");
                var dat = m_ConditionPanel.Do(this.DockPanel);
                this.m_MessageInterface.ShowInfo("执行完成。");
                string resultWinTxt;
                if (string.IsNullOrEmpty(m_ConditionPanel.ConditionPanelName))
                {
                    resultWinTxt = "执行结果";
                }
                else
                {
                    resultWinTxt = m_ConditionPanel.ConditionPanelName + "-执行结果";
                }
                m_ConditionPanel.ShowResult(dat, DockPanel, resultWinTxt);
                m_ConditionPanel.AfterCloseWithOK();
            }
            this.Close();
        }
    }
}
